﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PNet
{
    /// <summary>
    /// 建立Token对象缓存池
    /// </summary>
    public class IOCPTokenPool<T,K> where T : IOCPToken<K>,new()  where K : IOCPMsg,new()
    {
        public Stack<T> mTokenStacks;

        public IOCPTokenPool(int maxConut)
        {
            mTokenStacks = new Stack<T>(maxConut);
        }

        /// <summary>
        /// 取出Token对象
        /// </summary>
        /// <returns></returns>
        public T Pop()
        {
            lock (mTokenStacks)
            {
               return mTokenStacks.Pop();
            }
        }

        /// <summary>
        /// 存储Token对象
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public void Push(T token)
        {
            if (token == null) { IOCPTool.LogError("Push token is null:" + token); return; }
            lock (mTokenStacks)
            {
                mTokenStacks.Push(token);
            }
        }

    }
}
